﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Common.Filters;

public class LowerCaseDocumentFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        var pathsToRemove = new List<string>();

        foreach (var path in swaggerDoc.Paths)
        {
            var newPath = path.Key.ToLower();
            if (newPath != path.Key)
            {
                pathsToRemove.Add(path.Key);
            }
        }

        foreach (var pathKey in pathsToRemove)
        {
            var pathValue = swaggerDoc.Paths[pathKey];
            var newPath = pathKey.ToLower();

            swaggerDoc.Paths.Remove(pathKey);
            swaggerDoc.Paths.Add(newPath, pathValue);
        }
    }
}
